﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using SWF = System.Windows.Forms;
using System.Configuration;
using SharePointOptimizer.Model;
using System.Collections.ObjectModel;
using SharePointOptimizer.SQLServer;
using AG.Utilities;
using SharePointOptimizer.PL;
using SharePointOptimizer.PL.WPF.Datasets;
using SharePointOptimizer.PL.WPF.Datasets.DataSetWebApplicationsAndSiteCollectionsTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetServiceApplicationsTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetFarmOverviewTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetTopListsTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetTopSitesTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetSlowestTimerJobsTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetTopUsersTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets.DataSetTopPagesTableAdapters;
namespace SharePointOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for OverviewReportsControl.xaml
    /// </summary>
    public partial class OverviewReportsControl : UserControl
    {
        SharePointOptimizerDataContext dc;
        List<Service> SQLServerInstances;

        public OverviewReportsControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                if (MainWindow.IsDatabaseCreated())
                {
                }
            }
            catch (Exception sql)
            {
                Logger.Error(sql);


            }
        }

        private void cboReports_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }


        private void btnShowReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cboReports.SelectedIndex >= 0)
                {
                    String strReport = (cboReports.SelectedItem as ComboBoxItem).Content.ToString();

                    if (strReport == "WebApplications and SiteCollections")
                    {
                        ShowWebApplicationsAndSiteCollections();
                        //ShowServiceApplications2();
                    }
                    if (strReport == "Services Applications")
                    {
                        ShowServiceApplications();
                    }
                    if (strReport == "Farm overview")
                    {
                        ShowFarmOverview();
                    }
                    if (strReport == "Top Lists")
                    {
                        ShowTopLists();
                    }
                    if (strReport == "Top Sites")
                    {
                        ShowTopSites();
                    } 
                    if (strReport == "Slowest TimerJobs")
                    {
                        ShowSlowestTimerJobs();
                    }
                    if (strReport == "Most Active Users")
                    {
                        ShowTopUsers();
                    }
                    if (strReport == "Slowest Pages")
                    {
                        ShowTopPages();
                    }

                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }


        private void ShowWebApplicationsAndSiteCollections()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                WebApplicationsAndSiteCollectionsTableAdapter adapter = new WebApplicationsAndSiteCollectionsTableAdapter();
                
                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetWebApplicationsAndSiteCollections dataset = new DataSetWebApplicationsAndSiteCollections();

                adapter.Fill(dataset.WebApplicationsAndSiteCollections);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "WebApplicationsAndSiteCollections";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "WebApplicationsAndSiteCollections";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\WebApplicationsAndSiteCollections2.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowTopUsers()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                TopUsersTableAdapter adapter = new TopUsersTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetTopUsers dataset = new DataSetTopUsers();

                adapter.Fill(dataset.TopUsers);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "TopUsers";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "TopUsers";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\TopUsers.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowTopPages()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                TopPagesTableAdapter adapter = new TopPagesTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetTopPages dataset = new DataSetTopPages();

                adapter.Fill(dataset.TopPages);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "TopPages";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "TopPages";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\TopPages.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowSlowestTimerJobs()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                SlowestTimerJobsTableAdapter adapter = new SlowestTimerJobsTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetSlowestTimerJobs dataset = new DataSetSlowestTimerJobs();

                adapter.Fill(dataset.SlowestTimerJobs);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "SlowestTimerJobs";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "DataSetSlowestTimerJobs";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\SlowestTimerJobs.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowTopSites()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                TopSitesTableAdapter adapter = new TopSitesTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetTopSites dataset = new DataSetTopSites();

                adapter.Fill(dataset.TopSites);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "TopSites";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "TopSites";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\TopSites.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowTopLists()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                TopListsTableAdapter adapter = new TopListsTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetTopLists dataset = new DataSetTopLists();

                adapter.Fill(dataset.TopLists);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "TopLists";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "TopLists";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\TopLists.rdlc";
                viewer.RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }
        private void ShowServiceApplications()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                ServiceApplicationsTableAdapter adapter = new ServiceApplicationsTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetServiceApplications dataset = new DataSetServiceApplications();

                adapter.Fill(dataset.ServiceApplications);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "ServiceApplications";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "ServiceApplications";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\ServiceApplications.rdlc";
                viewer.RefreshReport();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
        /*private void ShowServiceApplications2()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                SharePointOptimizer.PL.WPF.Datasets.SharePointOptimizerDataSetTableAdapters.ReportProcedureTableAdapter adapter = new SharePointOptimizer.PL.WPF.Datasets.SharePointOptimizerDataSetTableAdapters.ReportProcedureTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                SharePointOptimizerDataSet dataset = new SharePointOptimizerDataSet();

                adapter.Fill(dataset.ReportProcedure);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "ReportProcedure";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "ReportProcedure";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\Report1.rdlc";
                viewer.RefreshReport();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }*/
        private void ShowFarmOverview()
        {
            try
            {
                ReportViewer viewer = (ReportViewer)wfh_report.Child;
                FarmOverviewTableAdapter adapter = new FarmOverviewTableAdapter();

                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetFarmOverview dataset = new DataSetFarmOverview();

                adapter.Fill(dataset.FarmOverview);


                SWF.BindingSource bindingSource = new SWF.BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "FarmOverview";


                ReportDataSource reportDataSource = new ReportDataSource();

                ////reportDataSource.Name = "IssuesReportDataset";
                reportDataSource.Name = "FarmOverview";
                reportDataSource.Value = bindingSource;

                viewer.LocalReport.DataSources.Clear();
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = SWF.BorderStyle.None;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\FarmOverview.rdlc";
                viewer.RefreshReport();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }



    }
}
